package io.renren.modules.bs.controller;

import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.bs.entity.BsDepartmentEntity;
import io.renren.modules.bs.service.BsDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 部门表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-08-13 22:35:07
 */
@RestController
@RequestMapping("/bs/department")
public class BsDepartmentController {

    @Autowired
    private BsDepartmentService bsDepartmentService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bsDepartmentService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{departmentId}")
    public R info(@PathVariable("departmentId") Long departmentId){
		BsDepartmentEntity bsDepartment = bsDepartmentService.getById(departmentId);

        return R.ok().put("department", bsDepartment);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BsDepartmentEntity bsDepartment){

        try {
            bsDepartmentService.saveModel(bsDepartment);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BsDepartmentEntity bsDepartment){
        try {
            bsDepartmentService.updateModel(bsDepartment);

            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] departmentIds){
        try {
            bsDepartmentService.removeRecords(Arrays.asList(departmentIds));
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @RequestMapping("/getListAndHospital")
    public R getListAndHospital(){

        JSONObject data = bsDepartmentService.getListAndHospital();
        return R.ok(data);
    }




    /**
     * 禁用或启用
     */
    @RequestMapping("/prohibitOrEnable")
    public R prohibitOrEnable(Integer id,Integer isFrozen){

        try {
            this.bsDepartmentService.prohibitOrEnable(id,isFrozen);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 获得列表
     */
    @RequestMapping("/getListByHospitalId")
    public R getListByHospitalId(Long hospitalId){
        List<BsDepartmentEntity> list = this.bsDepartmentService.getListByHospitalIds(Arrays.asList(hospitalId));
        return R.ok().put(list);
    }

}
